package com.dlwx.firefighting.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.DialogUtils;
import com.dlwx.firefighting.utils.KeyBoardUtils;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.flyco.dialog.entity.DialogMenuItem;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 提现界面
 */
public class WithDrawalActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.tv_blank)
    TextView tv_blank;
    @BindView(R.id.tv_balance_now)
    TextView tv_balance_now;
    @BindView(R.id.btn_begin_withdraw)
    TextView btn_begin;
    @BindView(R.id.btn_all_withdrawal)
    TextView btnAllWithdrawal;
    @BindView(R.id.money_et)
    EditText moneyEt;
    @BindView(R.id.bank_num_et)
    EditText bankNumEt;
    @BindView(R.id.bank_num_al)
    AutoLinearLayout bankNumAl;
    @BindView(R.id.withdraw_to_right)
    ImageView withdrawToRight;
    @BindView(R.id.select_blank_al)
    AutoRelativeLayout selectBlankAl;
    @BindView(R.id.remark_et)
    EditText remarkEt;
    @BindView(R.id.remark_al)
    AutoLinearLayout remarkAl;
    @BindView(R.id.bank_num_tv)
    TextView bankNumTv;
    @BindView(R.id.title_right_one_tv)
    TextView titleRightOneTv;

    private ArrayList<DialogMenuItem> mMenuItems = new ArrayList<>();
    private String balance;
    private String userId;
    private int account_type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_with_drawal);
        ButterKnife.bind(this);
        userId = (String) SPUtils.get(this, Constant.USER_ID, "");

        account_type = getIntent().getIntExtra("type", -1);
        String balance = getIntent().getStringExtra("balance");
        tv_balance_now.setText("当前余额" + balance + "元");
        initView();
    }

    private void initView() {
        titleRightOneTv.setVisibility(View.VISIBLE);
        titleRightOneTv.setText("提现密码");
        switch (account_type) {
            case Constant.ACCOUNT_Z:
                bankNumTv.setText("收款人账号：");
                bankNumTv.setHint("输入手机号码");
                selectBlankAl.setVisibility(View.GONE);
                btn_begin.setText("开始转账");
                break;
            case Constant.ACCOUNT_C:

                break;
            case Constant.ACCOUNT_T:
                bankNumTv.setText("银行卡号：");
                bankNumTv.setHint("输入银行卡号");
                btn_begin.setText("开始提现");
                selectBlankAl.setVisibility(View.VISIBLE);
                remarkAl.setVisibility(View.GONE);
                break;
        }
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText(getResources().getString(R.string.withdrawal));
      /*  edit_input.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEvent.ACTION_DOWN == event.getAction()) {
                    edit_input.setCursorVisible(true);
                }
                return false;
            }
        });*/

//        balance = (String) SPUtils.get(WithDrawalActivity.this, Constant.UserBalance, "0");


        mMenuItems.add(new DialogMenuItem("工商银行", R.mipmap.ic_launcher));
        mMenuItems.add(new DialogMenuItem("建设银行", R.mipmap.ic_launcher));
        mMenuItems.add(new DialogMenuItem("招商银行", R.mipmap.ic_launcher));
        mMenuItems.add(new DialogMenuItem("农业银行", R.mipmap.ic_launcher));
        mMenuItems.add(new DialogMenuItem("浦发银行", R.mipmap.ic_launcher));
    }

    @OnClick({R.id.title_left_one_btn, R.id.select_blank_al, R.id.btn_begin_withdraw,
            R.id.btn_all_withdrawal,R.id.title_right_one_tv})
    public void onViewClicked(View view) {
        KeyBoardUtils.closeKeybord(this);
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                finish();
                break;
            case R.id.title_right_one_tv:
                startActivity(new Intent(this,WithDrawPswActivity.class));
                break;
            case R.id.select_blank_al:
                startActivity(new Intent(this,AccountActivity.class));
                break;
            case R.id.btn_all_withdrawal:
                /*edit_input.setText(balance);
                //获取光标，打开软键盘
                edit_input.setCursorVisible(true);
                edit_input.setSelection(balance.length());
                KeyBoardUtils.openKeybord(edit_input, this);*/
                break;
            case R.id.btn_begin_withdraw:
                String money = moneyEt.getText().toString();
                String accountNum = bankNumEt.getText().toString();
                String remark = remarkEt.getText().toString();
                if (TextUtils.isEmpty(money)) {
                    ToastUtils.toastForShort(WithDrawalActivity.this, "金额不能为空");
                    return;
                }
                if (TextUtils.isEmpty(accountNum)) {
                    ToastUtils.toastForShort(WithDrawalActivity.this, "账户不能为空");
                    return;
                }
                PostRequest<String> request = OkGo.<String>post(Constant.TRANSFER_ACCOUNTS).tag(this);
                switch (account_type) {
                    case Constant.ACCOUNT_Z:
                        request.params("user_id", userId)
                                .params("mobile_phone", accountNum)
                                .params("money", money)
                                .params("beizhu", remark);
                        doMoney(request);
                        break;
                    case Constant.ACCOUNT_C:
                        break;
                    case Constant.ACCOUNT_T:
                        String bank = tv_blank.getText().toString();
                        request.params("user_id", userId)
                                .params("num", accountNum)
                                .params("amount", money)
                                .params("payment", bank);
                        doMoney(request);
                        break;
                }
                break;
        }
    }

    private void doMoney(PostRequest<String> request) {
        DialogUtils.showProgressDlg(this, "");
        request.execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                DialogUtils.stopProgressDlg();
//                DealResult dealResult = new DealResult();
//                BaseBean bean = dealResult.dealBaseRes(WithDrawalActivity.this, response);
//                if (bean != null) {
//                    if (bean.getStatus() == Constant.SUCESS) {
//                        tv_blank.setText("");
//                        moneyEt.setText("");
//                        bankNumEt.setText("");
//                        remarkEt.setText("");
//                    }
//                    ToastUtils.toastForShort(WithDrawalActivity.this, bean.getMsg());
//                }

            }

            @Override
            public void onError(Response<String> response) {
                DialogUtils.stopProgressDlg();
                ToastUtils.toastForShort(WithDrawalActivity.this, getString(R.string.bad_net));
                super.onError(response);
            }
        });
    }

    //-------------------------------------------------
}
